package com.kun.comic.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.kun.comic.extra.NavigationDrawerCallbacks;
import com.kun.comic.model.NavigationItem;
import com.kun.comic.R;

public class NavigationDrawerAdapter extends
		RecyclerView.Adapter<NavigationDrawerAdapter.ViewHolder> {

	private List<NavigationItem> mData;
	private NavigationDrawerCallbacks mNavigationDrawerCallbacks;
	private int mSelectedPosition;
	private int mTouchedPosition = -1;

	public NavigationDrawerAdapter(List<NavigationItem> data) {
		mData = data;
	}

	public NavigationDrawerCallbacks getNavigationDrawerCallbacks() {
		return mNavigationDrawerCallbacks;
	}

	public void setNavigationDrawerCallbacks(
			NavigationDrawerCallbacks navigationDrawerCallbacks) {
		mNavigationDrawerCallbacks = navigationDrawerCallbacks;
	}

	@Override
	public NavigationDrawerAdapter.ViewHolder onCreateViewHolder(
			ViewGroup viewGroup, int i) {
		View v = LayoutInflater.from(viewGroup.getContext()).inflate(
				R.layout.drawer_row, viewGroup, false);
		final ViewHolder viewHolder = new ViewHolder(v);
		viewHolder.itemView.setOnTouchListener(new View.OnTouchListener() {
			@SuppressLint("ClickableViewAccessibility")
			@Override
			public boolean onTouch(View v, MotionEvent event) {

				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					touchPosition(viewHolder.getPosition());
					return false;
				case MotionEvent.ACTION_CANCEL:
					touchPosition(-1);
					return false;
				case MotionEvent.ACTION_MOVE:
					return false;
				case MotionEvent.ACTION_UP:
					touchPosition(-1);
					return false;
				}
				return true;
			}
		});
		viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mNavigationDrawerCallbacks != null)
					mNavigationDrawerCallbacks
							.onNavigationDrawerItemSelected(viewHolder
									.getPosition());
			}
		});

		return viewHolder;
	}

	@Override
	public void onBindViewHolder(NavigationDrawerAdapter.ViewHolder viewHolder,
			final int i) {
		viewHolder.textView.setText(mData.get(i).getText());
		viewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(mData
				.get(i).getDrawable(), null, null, null);

		// TODO: selected menu position, change layout accordingly
		if (mSelectedPosition == i || mTouchedPosition == i) {
			viewHolder.itemView.setBackgroundColor(viewHolder.itemView
					.getContext().getResources()
					.getColor(R.color.selected_gray));
		} else {
			viewHolder.itemView.setBackgroundColor(Color.TRANSPARENT);
		}
	}

	private void touchPosition(int position) {
		int lastPosition = mTouchedPosition;
		mTouchedPosition = position;
		if (lastPosition >= 0)
			notifyItemChanged(lastPosition);
		if (position >= 0)
			notifyItemChanged(position);
	}

	public void selectPosition(int position) {
		int lastPosition = mSelectedPosition;
		mSelectedPosition = position;
		notifyItemChanged(lastPosition);
		notifyItemChanged(position);
	}

	@Override
	public int getItemCount() {
		return mData != null ? mData.size() : 0;
	}

	public static class ViewHolder extends RecyclerView.ViewHolder {
		public TextView textView;

		public ViewHolder(View itemView) {
			super(itemView);
			textView = (TextView) itemView.findViewById(R.id.item_name);
		}
	}
}
